#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN=1e5+10;
int a[MAXN];
int b[MAXN];
int dpa[MAXN];
int dpb[MAXN];
int main(void){
    int t,n,m;
    scanf("%d",&t);
    while(t--) {
        scanf("%d %d", &n, &m);
        memset(dpa, 0, sizeof dpa);
        memset(dpb, 0, sizeof dpb);
        for(int i=0; i<n; ++i) {
            scanf("%d", &a[i]);
            dpa[a[i]] = dpa[a[i]-1] + 1;
        }
        for(int i=0; i<m; ++i) {
            scanf("%d", &b[i]);
            dpb[b[i]] = dpb[b[i]-1] + 1;
        }
        int ans = 0;
        for(int i=1; i<=MAXN; ++i) {
            ans = max(ans, min(dpa[i], dpb[i]));
        }
        printf("%d\n", ans);
    }
    return 0;
}
